package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhit.collaborative_learning.dao.BBS_ClassDao;
import com.hhit.collaborative_learning.dao.BBS_SpecialDao;
import com.hhit.collaborative_learning.dao.impl.BBS_ClassDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_SpecialDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.mysql.jdbc.StringUtils;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class PutSorCServlet
 */
@WebServlet("/community/PutSorCServlet")
public class PutSorCServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PutSorCServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String tyle = request.getParameter("tyle");
		Connection con = DBAccess.getInstance().getConn();
		JSONObject jobj = new JSONObject();
		switch (tyle) {
		case "0":
			System.out.println("类型  " + tyle + " 添加Special");
			String stitle = request.getParameter("titleid");
			jobj = addSpecial(con, stitle);
			break;
		case "1":
			System.out.println("类型  " + tyle + " 添加Class");
			String ctitle = request.getParameter("ctitle");
			String cIntro = request.getParameter("cIntro");
			String crule = request.getParameter("crule");
			String icon_select = request.getParameter("icon_select");
			String sp_id = request.getParameter("sp_id");
			jobj = addClass(con, ctitle, cIntro, crule, icon_select, sp_id);
			break;
		default:
			System.out.println("非法类型  " + tyle);
			break;
		}
		try {
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			System.out.println("json  " + jobj.toString());
			DBAccess.getInstance().close(con, null, null);
		}
	}

	private JSONObject addClass(Connection con, String ctitle, String cIntro,
			String crule, String icon_select, String sp_id) {
		// TODO Auto-generated method stub
		String isSuccess = "false";
		BBS_ClassDao cd = new BBS_ClassDaoImpl();
		JSONObject jobj = new JSONObject();
		int re = 0;
		if (!StringUtils.isNullOrEmpty(ctitle)
				&& !StringUtils.isNullOrEmpty(cIntro)
				&& !StringUtils.isNullOrEmpty(crule)
				&& !StringUtils.isNullOrEmpty(icon_select)) {
			re = cd.insert(con, ctitle, cIntro, crule, icon_select, sp_id);
			if (re != 0) {
				isSuccess = "true";
			}
		}
		jobj.put("result", re);
		jobj.put("isSuccess", isSuccess);
		return jobj;
	}

	private JSONObject addSpecial(Connection con, String stitle) {
		// TODO Auto-generated method stub
		String isSuccess = "false";
		JSONObject jobj = new JSONObject();
		int re = 0;
		BBS_SpecialDao sd = new BBS_SpecialDaoImpl();
		if (!StringUtils.isNullOrEmpty(stitle)) {
			re = sd.insert(con, stitle);
			if (re != 0) {
				isSuccess = "true";
			}
		}
		jobj.put("result", re);
		jobj.put("isSuccess", isSuccess);
		return jobj;
	}

}
